package com.qzct.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;

import com.qzct.entity.Admin;
import com.qzct.entity.Email;
//import com.qzct.entity.dto.AdminLoginDto;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import com.qzct.exceptions.ApiException;
import com.qzct.service.AdminService;
import com.qzct.service.EmailService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@Api(tags = "发送邮箱")
@RequestMapping("/email")
public class AuthController {
    @Value("${mycuo.default-salt}")//自定义配置属性
    private String salt;
    @Autowired
    private EmailService emailService;

    @Autowired
    private AdminService adminService;
//    @PostMapping("/admin/login")
//    public Admin login(@Validated AdminLoginDto adminLoginDto,HttpServletRequest request){
//        //判断验证码是否错误
//        String verifyCode=adminLoginDto.getVerifyCode().toLowerCase();
//        HttpSession session=request.getSession();
//        String key= session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString().toLowerCase();
//        if(!verifyCode.equals(key)){
//            throw new ApiException(ResultCode.VERIFY_CODE_FAILURE);
//        }
//        String password=adminLoginDto.getPassword();
//
//        String md5Password= StringUtil.MD5(password+salt);
//
//        Admin admin=adminService.login(adminLoginDto.getAdminName(),md5Password);
//
//        // 判断管理员登陆是否成功 失败 重定向到登陆页，
//        if(admin==null){
//            throw new ApiException(ResultCode.USER_LOGIN_ERROR);
//
//        }
//        return admin;
//
//    }

//    @GetMapping("/captcha.jpg")
//    public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        response.setHeader("Cache-Control", "no-store, no-cache");
//        response.setContentType("image/jpeg");
//        //生成文字验证码
//        String text = defaultKaptcha.createText();
//        //生成图片验证码
//        BufferedImage image = defaultKaptcha.createImage(text);
//        HttpSession session=request.getSession();
//        //把验证码保存到session
//        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
//
//        ServletOutputStream out = response.getOutputStream();
//        ImageIO.write(image, "jpg", out);
//    }
    @GetMapping("/sendValidateCode")
    public ResultVo sendValidateCode(String mail,String name){
        String[] emails={mail};
        if(StringUtils.isEmpty(mail)){
            throw  new ApiException(ResultCode.CUSTOM_ERROR,"邮件接收者不能为空");
        }

        try {
            Email email=new Email();
            email.setUser(emails);


            email.setSubject("泉州文旅反馈结果");

            email.setContent("您在"+name+"提到的问题已经得到解决,感谢您的反馈!");

            emailService.sendMail(email);
            return new ResultVo(ResultCode.SUCCESS);
        }
        catch (Exception e){

            throw  new ApiException(ResultCode.CUSTOM_ERROR,"邮件发送失败");
        }

    }
    @GetMapping("/forget")
    public ResultVo sendValidateForgetCode(String mail){
        String[] emails={mail};
        if(StringUtils.isEmpty(mail)){
            throw  new ApiException(ResultCode.CUSTOM_ERROR,"邮件接收者不能为空");
        }

        try {
            Email email=new Email();
            email.setUser(emails);


            email.setSubject("邮箱验证码");

            email.setContent("修改密码的验证码E121,请不要泄露给别人!");

            emailService.sendMail(email);
            return new ResultVo(ResultCode.SUCCESS);
        }
        catch (Exception e){

            throw  new ApiException(ResultCode.CUSTOM_ERROR,"邮件发送失败");
        }

    }
}
